local skynet = require "skynet"
local tabletool = require "tabletool"
local msghelper = require "tablehelper"
local filelog = require "filelog"
local timer = require "timer"
local timetool = require "timetool"
require "ddzenum"

local RoomSeatLogic = {}

function RoomSeatLogic.init(seatobj, index)
	seatobj.index = index
	seatobj.state = ESeatState.SEAT_STATE_NO_PLAYER
	seatobj.is_tuoguan = EBOOL.FALSE
	return true
end

function RoomSeatLogic.clear(seatobj)
	seatobj.rid = 0
	seatobj.state = 0  --改坐位玩家状态
	seatobj.playerinfo = {}
end

function RoomSeatLogic.is_empty(seatobj)
	return (seatobj.state == ESeatState.SEAT_STATE_NO_PLAYER)
end

function RoomSeatLogic.dealcards(seatobj, turns)
	seatobj.handcards = tabletool.deepcopy(seatobj.cards)
	local noticemsg = {
		rid = seatobj.rid,
		seat_index = seatobj.index,
		cards = tabletool.deepcopy(seatobj.cards),
		turns = turns,
	}
	msghelper:sendmsg_to_tableplayer(seatobj, "cardinfoN",noticemsg)

	noticemsg.cards = {}
	msghelper:sendmsg_to_allwaitplayer("cardinfoN", noticemsg)
end

function RoomSeatLogic.dealtuoguan(gameobj,seat)
	local server = msghelper.get_server()
	if seat.is_tuoguan == EBOOL.FALSE then
		seat.is_tuoguan = EBOOL.TRUE
		local noticemsg = {
			rid = seat.rid,
			seat_index = seat.index,
			action_type = EActionType.ACTION_TYPE_REQUEST_TUOGUAN,
			cards = {},
		}
		msghelper:sendmsg_to_alltableplayer("doactionN", noticemsg)
		
		----如果已经注册玩家操作超时定时器,则取消
		if gameobj.action_seat_index == seat.index and gameobj.state ~= ETableState.TABLE_STATE_WAIT_PLAYER_DOUBLE
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END_AFTER 
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_REAL_END and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END 
			and gameobj.state ~= ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END then
			if gameobj.timer_id and gameobj.timer_id > 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				timer.cleartimer(gameobj.timer_id)
				gameobj.timer_id = -1
			end
			local doactionntcmsg = {
				rid = gameobj.seats[gameobj.action_seat_index].rid,
				seat_index = gameobj.action_seat_index,
				action_type = gameobj.action_type,
				action_to_time = gameobj.action_to_time,
			}
			local lefttime = gameobj.action_to_time - timetool.get_time()
			if lefttime <= gameobj.conf.tuoguan_action_time and lefttime >= 0 then
				gameobj.timer_id = timer.settimer(lefttime*100, "doaction", doactionntcmsg)
			else
				gameobj.timer_id = timer.settimer(gameobj.conf.tuoguan_action_time*100, "doaction", doactionntcmsg)
			end
		end
	end
end

function RoomSeatLogic.canceltuoguan(gameobj,seat)
	local server = msghelper.get_server()
	if seat.is_tuoguan == EBOOL.TRUE then
		seat.timeout_count = 0
		seat.is_tuoguan = EBOOL.FALSE
		seat.tuoguan_punish = 0
		local noticemsg = {
			rid = seat.rid,
			seat_index = seat.index,
			action_type = EActionType.ACTION_TYPE_CANCEL_TUOGUAN,
			cards = {},
		}
		msghelper:sendmsg_to_alltableplayer("doactionN", noticemsg)
		----如果已经注册玩家操作超时定时器,则取消,在结算阶段不取消定时器,防止误取消定时器结算阶段中断.
		if gameobj.action_seat_index == seat.index and gameobj.state ~= ETableState.TABLE_STATE_WAIT_PLAYER_DOUBLE
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END_AFTER 
			and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_REAL_END and gameobj.state ~= ETableState.TABLE_STATE_ONE_GAME_END 
			and gameobj.state ~= ETableState.TABLE_STATE_WAIT_ONE_GAME_REAL_END then
			if gameobj.timer_id and gameobj.timer_id > 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				timer.cleartimer(gameobj.timer_id)
				gameobj.timer_id = -1
			end
			local doactionntcmsg = {
				rid = gameobj.seats[gameobj.action_seat_index].rid,
				seat_index = gameobj.action_seat_index,
				action_type = gameobj.action_type,
				action_to_time = gameobj.action_to_time,
			}
			local lefttime = gameobj.action_to_time - timetool.get_time()
			if lefttime >= 0 and not server.logic.tablelogic.is_onegameend(gameobj) then
				gameobj.timer_id = timer.settimer(lefttime*100, "doaction", doactionntcmsg)
			end
		end
	end
end

function RoomSeatLogic.setSeatstate(gameobj)
	if gameobj.action_type == EActionType.ACTION_TYPE_CHUPAI then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_CHUPAI
	elseif gameobj.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_FOLLOW_CHUPAI
	elseif gameobj.action_type == EActionType.ACTION_TYPE_CHECK then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_CHECK
	elseif gameobj.action_type == EActionType.ACTION_TYPE_JIAODIZHU then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_JDZ
	elseif gameobj.action_type == EActionType.ACTION_TYPE_QIANGDIZHU then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_QIANGDZ
	elseif gameobj.action_type == EActionType.ACTION_TYPE_BUJIAO_DIZHU then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_NOT_JDZ
	elseif gameobj.action_type == EActionType.ACTION_TYPE_BUQIANGDIZHU then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_NOT_QIANGDZ
	elseif gameobj.action_type == EActionType.ACTION_TYPE_DO_DOUBLE then
		gameobj.seats[gameobj.action_seat_index].state = ESeatState.SEAT_STATE_NOT_QIANGDZ
	end
end

function RoomSeatLogic.resetstate(seat)
	seat.state = ESeatState.SEAT_STATE_WAIT_READY
	seat.timeout_count = 0
	seat.is_tuoguan = EBOOL.FALSE
	seat.win = 0		---表示玩家胜利还是失败
	seat.jdztag = 0  	----记录叫地主标识(不叫地址值为0, 1表示叫地主, 2表示抢地主)
	seat.isdz = EPlayerIDType.DDZ_PLAYER_ID_TYPE_UNKNOW ---记录是否是地主
	seat.is_ready = EBOOL.FALSE
	seat.ready_timer_id = -1 ---准备倒计时定时器
	seat.ready_to_time = 0   ---准备到期时间
	seat.cards = {}   ---玩家手牌
	seat.handcards = {} --初始手牌
	seat.multiple = 0		 ----玩家加倍倍数
	seat.balance_score = 0	 ----最终结算分数
	seat.tuoguan_punish = 0		-------
	seat.followhandsnum = 0
	seat.bomboff = nil
	seat.diamond_base = nil
	seat.standup = nil
	seat.start_diamond = nil
	seat.doubled = 0
	seat.handsround = 0
end

function RoomSeatLogic.init_seat(seat)
	seat.state = ESeatState.SEAT_STATE_WAIT_START
	seat.jdztag = 0
	seat.doubled = 0
	seat.ready_timer_id = -1 ---准备倒计时定时器
	seat.ready_to_time = 0   ---准备到期时间
	seat.cards = {}   ---玩家手牌
	seat.handcards = {} --初始手牌
	seat.handsround = 0
	if seat.ready_timer_id and seat.ready_timer_id > 0  then ---取消准备倒计时，以防万一
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
		seat.ready_to_time = 0
	end
end

function RoomSeatLogic.clear_seat(seat)
	seat.rid = 0
	seat.state = ESeatState.SEAT_STATE_NO_PLAYER
	seat.playerinfo = {}
	seat.is_tuoguan = EBOOL.FALSE
	seat.timeout_count = 0
	seat.win = 0
	seat.jdztag = 0
	seat.isdz = EPlayerIDType.DDZ_PLAYER_ID_TYPE_UNKNOW
	seat.diamond = 0
	seat.getdiamond = 0
	seat.fangka = 0
	seat.multiple = 0		----玩家加倍倍数
	seat.ready_to_time = 0
	seat.cards = nil
	seat.handcards = nil --初始手牌
	seat.is_disconnect = nil
	seat.followhandsnum = 0
	seat.bomboff = nil
	seat.diamond_base = nil
	seat.standup = nil
	seat.start_diamond = nil
	seat.doubled = nil
	seat.handsround = 0
	seat.sametabledata = nil
	seat.rematched = true
	if seat.ready_timer_id and seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
end

function RoomSeatLogic.onegamestart_initseat(seat)
	seat.state = ESeatState.SEAT_STATE_PLAYING
	seat.ready_to_time = 0
	if seat.ready_timer_id and seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
	seat.diamond_base = seat.getdiamond
end

return RoomSeatLogic